package api

import (
	"api_getway/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Upload(c *gin.Context) {
	file, err := c.FormFile("image")
	if err != nil {
		return
	}

	if file.Size > 200*1024 {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"mes":  "图片大小不能超过200kb",
		})
		return
	}

	etx := file.Filename
	if etx != "png" {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"mes":  "图片格式必须是png",
		})
		return
	}

	upload, err := pkg.Upload(file)
	if err != nil {
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "图片上传成功",
		"data": upload,
	})
}
